package com.spongeli.user.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.spongeli.user.service.IFileUploadService;

@Controller
@RequestMapping("file")
public class FileController {
	@Resource
	private IFileUploadService fileUploadService;
	/**
	 * 文件下载
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author spongeli
	 */
	@RequestMapping("download")
	public void download(HttpServletRequest request,HttpServletResponse  response) throws Exception{
		
		response.setContentType("text/html;charset=UTF-8");   
        BufferedInputStream in = null;  
        BufferedOutputStream out = null;  
        request.setCharacterEncoding("UTF-8");  
        String rootpath = request.getSession().getServletContext().getRealPath("/");  
      // fileName=CommonProperty.getValue(fileName);  
        try {  
            File f = new File(rootpath + "excel/销售线索导入模板.xls");
            if(f !=null && f.exists() ){
            	String fileName = f.getName();
	            response.setContentType("application/x-excel");  
	            response.setCharacterEncoding("UTF-8");  
	            response.setHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));  
	            response.setHeader("Content-Length",String.valueOf(f.length()));  
	            in = new BufferedInputStream(new FileInputStream(f));  
	            out = new BufferedOutputStream(response.getOutputStream());  
	            byte[] data = new byte[1024];  
	            int len = 0;  
	            while (-1 != (len=in.read(data, 0, data.length))) {  
	                out.write(data, 0, len);  
	            } 
	            System.out.println("文件下载成功！");
	            out.flush();
            }else{
            	throw new Exception("文件不存在！");
            }
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {           
            	if (in != null) in.close();  
            	if (out != null) out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}  
        }  
    }  
	/**
	 * 文件上传 解析
	 * @param request
	 * @return
	 * @author spongeli
	 */
	@RequestMapping(value = "upload",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String upload(HttpServletResponse response,HttpServletRequest request,@RequestParam("file") MultipartFile  file){
        Map<String, Object> map = new HashMap<String, Object>();
        if(null == file || file.getSize()==0){   //文件不存在的情况
            map.put("status", "300");
            map.put("msg", "上传文件不存在或为空文件");
            return JSON.toJSONString(map);  //返回错误信息
        }
        //获取服务器 中file/update 的 url地址        return map;
        String targetPath = request.getServletContext().getRealPath("/file/upload"); 
        System.out.println(targetPath);
        try {
			Map<String,Object> res = this.fileUploadService.uploadExcel(targetPath,file);
			map.put("status", "200");
			map.put("data", res);
			map.put("msg", "上传成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
        return JSON.toJSONString(map) ;
	}

}
